$(function() {

	var url = "/mysjshop/AddressServlet?flag=province";
	var data = "";
	$.post(url, data, function(map) {
		var list = map.list;
		var provinceHtml = "<option value='-1' >省/直辖市</option>";
		for (var i = 0; i < list.length; i++) {
			var pro = list[i];
			provinceHtml += "<option  value='" + pro.provinceID + "'>"
					+ pro.province + "</option>";
		}
		$("#province").html(provinceHtml);
	}, "json");
	// 选中省后动态显示市
	$("#province").change(
			function() {
				// 先将市的之前的信息清除
				$("#city").html("<option value='-1'>请选择市</option>");
				// 再将区的信息清除
				$("#area").html("<option value='-1'>请选择区</option>");
				// 再将用户的输入清除
				$("#address").val("");
				var proValue = $("#province").val();
				var url = "/mysjshop/AddressServlet?flag=city";
				var data = {
					provinceId : proValue
				};
				$.post(url, data, function(map) {
					var list1 = map.cityList;
					var cityHtml = "<option  value='-1'>请选择市</option>";
					for (var i = 0; i < list1.length; i++) {
						var city = list1[i];
						cityHtml += "<option value='" + city.cityID + "'>"
								+ city.cityName + "</option>";
					}
					$("#city").html(cityHtml);
				}, "json");
			});
	// 选中市后 动态显示地区
	$("#city").change(
			function() {
				var cityValue = $("#city").val();
				var url = "/mysjshop/AddressServlet?flag=area";
				var data = {
					cityId : cityValue
				};
				$.post(url, data, function(map) {
					var list = map.list;
					var areaHtml = "<option  value='-1'>请选择地区</option>";
					for (var i = 0; i < list.length; i++) {
						var area = list[i];
						areaHtml += "<option value='" + area.areaID + "'>"
								+ area.areaName + "</option>";
					}
					$("#area").html(areaHtml);
				}, "json");
			});

});

function validateIphone() {
	var regl = /^\d{11}$/;
	var read = false;
	if (regl.test($('#iphone').val())) {
		$('#ts1').html(' ');
		read = true;
	} else {
		$('#ts1').html('请输入正确格式手机号');

		read = false;
	}
	return read;
}
function validateShName() {
	var regExp = /^[A-Za-z1-9\u4e00-\u9fa5]+$/;
	var reg = /^[A-Za-z1-9\u4e00-\u9fa5]{1,15}$/;
	if ($('#shName').val() != "") {
		if (regExp.test($('#shName').val())) {
			if (reg.test($('#shName').val())) {
				$("#ts1").html("");
				read = true;
			} else {
				$("#ts1").html('姓名长度超过15个字');
				read = false;
			}
		} else {
			$("#ts1").html('姓名含有非法字符');
			read = false;
		}
	} else {
		$("#ts1").html("姓名不能为空");
		read = true;
	}
	return read;
}
function validateXxdz() {
	var regl = /^[A-Za-z1-9\u4e00-\u9fa5]{4,50}$/;
	var read = false;
	if (regl.test($('#xxdz').val())) {
		$('#ts1').html(' ');
		read = true;
	} else {
		$('#ts1').html('详细地址不能小于4或超过50个字');

		read = false;
	}
	return read;
}
function validateForm() {
	if (validateIphone() && validateShName() && validateXxdz()) {
		var url = "/mysjshop/AddressServlet?flag=addAddress"
		var data = $("#addAddress").serialize();
		$.post(url, data, function(rs) {
			if (rs) {
				alert("添加成功")
				location = "/mysjshop/pages/nowBuy.html";
			} else {
				alert("添加失败，请填写正确的收货地址");
			}
		}, "json");
	} else {
		$('#ts1').html('请填写正确的收货地址');
	}
}

function onclick_close() {
	var shade_content = $(".shade_content");
	var shade = $(".shade");
	if (confirm("确认关闭么！此操作不可恢复")) {
		shade_content.hide();
		shade.hide();
	}
}

function onclick_open() {
	$(".shade_content").show();
	$(".shade").show();
	var input_out = $(".input_style");
	for (var i = 0; i <= input_out.length; i++) {
		if ($(input_out[i]).val() != "") {
			$(input_out[i]).val("");
		}
	}
}

$(function() {
	listPager();
	$('#bc').click(function() {
		validateForm();
	});
	$('#iphone').blur(function() {
		validateIphone();
	});
	$('#shName').blur(function() {
		validateShName();
	});
	$('#xxdz').blur(function() {
		validateXxdz();
	});
});